package com.devunion.salon.client.app.mvc.view;

import com.devunion.salon.client.Constants;
import com.devunion.salon.client.common.ProductList;
import com.devunion.salon.client.app.bean.ProductModel;
import com.devunion.salon.client.app.bean.search.ProductSearchPagingLoadConfig;
import com.devunion.salon.client.app.mvc.SalonEvents;
import com.devunion.salon.client.rpc.ProductService;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.data.*;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.*;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spetrakovsky
 */
public class ProductListView extends View {

    private ProductList productList;

    private boolean initialized = false;

    public ProductListView(Controller controller) {
        super(controller);
    }

    protected void handleEvent(AppEvent<?> event) {
        switch (event.type) {
            case SalonEvents.NAVIGATE_PRODUCT_LIST_VIEW:
                if (!initialized) {
                    initComponent();
                }
                renderComponent();
                break;
        }
    }

    private void initComponent() {
        productList = new ProductList();
        productList.initialize();
        productList.getProductGrid().addListener(Events.CellDoubleClick, new Listener<GridEvent>() {
            public void handleEvent(GridEvent ge) {
                ProductModel productModel = productList.getProductGrid().getStore().getAt(ge.rowIndex);
                fireEvent(new AppEvent<ProductModel>(SalonEvents.NAVIGATE_PRODUCT_VIEW, productModel));
            }
        });
        initialized = true;
    }

    private void renderComponent() {
        ContentPanel centerPanel = Registry.get(Constants.APP_CENTER_PANEL);
        centerPanel.removeAll();
        centerPanel.add(productList);
        productList.getToolBar().refresh();
        centerPanel.layout();
    }

}
